package com.example.springboot.controller;

import cn.hutool.core.collection.CollUtil;
import com.example.springboot.Service.ICategoryService;
import com.example.springboot.Service.IUserService;
import com.example.springboot.common.Result;
import com.example.springboot.controller.request.CategoryPageRequest;
import com.example.springboot.controller.request.UserPageRequest;
import com.example.springboot.entity.Book;
import com.example.springboot.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    ICategoryService iCategoryService;

    //四种注解表示，新增查询，删除，修改
    @PostMapping("/save")
    //@RequestBody 把Jason转换成User对象
    public Result save(@RequestBody Category category) {
        iCategoryService.save(category);
        return Result.success();
    }

    @PutMapping("/update")
    //@RequestBody 把Jason转换成User对象
    public Result update(@RequestBody Category category) {
        iCategoryService.update(category);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Category users = iCategoryService.getById(id);
        return Result.success(users);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        iCategoryService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        List<Category> users = iCategoryService.list();
        return Result.success(users);
    }

    @GetMapping("/tree")
    public Result tree() {
        List<Category> list = iCategoryService.list();
//        //对list操作即可
//        List<Category> treeList=list.stream().filter(v->v.getPid()==null).collect(Collectors.toList());//第一层
//        //比如递归实现children列表的查询
//
        return Result.success(createTree(null, list));//null表示从第一级开始递归
    }

    //通过完全递归方法，实现递归树,给分类增加树形结构
    //pid为上级的id
    private List<Category> createTree(Integer pid, List<Category> categories) {
        List<Category> treeList = new ArrayList<>();
        for (Category category : categories) {
            if (pid == null) {
                if (category.getPid() == null) {//为第一结点
                    treeList.add(category);
                    category.setChildren(createTree(category.getId(), categories));
                }
            } else {
                if (pid.equals(category.getPid())) {
                    treeList.add(category);
                    category.setChildren(createTree(category.getId(), categories));
                }
            }
            if (CollUtil.isEmpty(category.getChildren())) {
                category.setChildren(null);
            }
        }
        return treeList;
    }

    @GetMapping("/page")
    public Result page(CategoryPageRequest categoryPageRequest) {
        return Result.success(iCategoryService.page(categoryPageRequest));
    }
}
